package com.study.rabbitmq2.topics;

import com.rabbitmq.client.*;
import com.study.rabbitmq2.util.ConnectionUtil;

import java.io.IOException;

/**
 * @Author chenyeqing
 * @Date 2021/11/24 22:05
 */
public class Recv {
    private final static String QUEUE_NAME = "topic_exchange_queue_Q1";

    private final static String EXCHANGE_NAME = "test_topic_exchange";

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        // 绑定队列到交换机，同时指定需要订阅的routingKey。订阅关于兔子以及懒惰动物的消息
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.orange.*");

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("[消费者1] received：" + msg);
            }
        };
        // 监听队列，自动ACK
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
